package com.tianzeculture.stagebao.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;

/**
 * 范围选择
 */
public class RangePickerPopWindow extends PopupWindow implements OnWheelChangedListener {
    static final String TAG = "RangePickerPopWindow";

    protected int myMin = 0;//最小值
    protected int myMax = 0;//最大值

    private String divider = "-";
    //    private String DEFAULT_PICKVALUE = "不限-不限-不限";
    private String DEFAULT_PICKVALUE = "不限-不限";
    private String pickValue = DEFAULT_PICKVALUE;

    private Activity mContext;
    private WheelView wvMin;
    private WheelView wvMax;
    private final View cityPickerView;
    private final LayoutInflater inflater;
    private final Display display;
    private final WindowManager windowManager;
    private TextView closePopWindow;
    private TextView showValue;
    private RangePickListener rangePickListner;

    int[] ints = null;
    String[] minStrings, maxStrings;
    boolean showOther = true;

    public RangePickerPopWindow(Activity context, int[] ints, boolean showOther) {
        super(context);
        this.mContext = context;
        this.ints = ints;
        this.showOther = showOther;

        if (showOther)
            DEFAULT_PICKVALUE = "不限-不限";
        else
            DEFAULT_PICKVALUE = "不限";

        initProvinceDatas();

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        cityPickerView = inflater.inflate(R.layout.popwindow_range_picker, null);

        //设置CityPickerPopWindow的View
        this.setContentView(cityPickerView);
        //设置CityPickerPopWindow弹出窗体的宽
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
        this.setWidth(RelativeLayout.LayoutParams.FILL_PARENT);
        //设置CityPickerPopWindow弹出窗体的高
        this.setHeight((int) (display.getHeight() * 0.35));
        //设置CityPickerPopWindow弹出窗体可点击
        this.setFocusable(true);
        //设置CityPickerPopWindow可触摸
        this.setTouchable(true);
        //设置CityPickerPopWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x9000000);
        //设置CityPickerPopWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOutsideTouchable(true);
        setUpListener(cityPickerView);
        setUpData();
    }

    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
        } else {
            this.dismiss();
        }
    }

    /**
     * 设置地区选择器监听器
     *
     * @param cityPickerView
     */
    private void setUpListener(View cityPickerView) {
        closePopWindow = (TextView) cityPickerView.findViewById(R.id.closePop);
        showValue = (TextView) cityPickerView.findViewById(R.id.showValue);

        wvMin = (WheelView) cityPickerView.findViewById(R.id.wvMin);
        wvMax = (WheelView) cityPickerView.findViewById(R.id.wvMax);
        if (showOther)
            wvMax.setVisibility(View.VISIBLE);
        else
            wvMax.setVisibility(View.GONE);

        wvMin.addChangingListener(this);
        wvMax.addChangingListener(this);

        showValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rangePickListner != null) {
                    rangePickListner.pickValue(pickValue);
                }
                dismiss();
            }
        });
        closePopWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 初始化地区选择器数据
     */
    private void setUpData() {
        wvMin.setViewAdapter(new ArrayWheelAdapter<String>(mContext, minStrings));
        wvMin.setVisibleItems(7);
        wvMax.setVisibleItems(7);
        updateCities();

    }


    /**
     * 更新城市
     */
    private void updateCities() {
        int pCurrent = wvMin.getCurrentItem();
        myMin = ints[pCurrent];

        int begin = 0;//最大值开始索引
        for (int i = 0; i < ints.length; i++) {
            if (myMin == ints[i]) {
                begin = i;
            }

            maxStrings = new String[ints.length - begin];
            if (myMin < ints[i]) {
                maxStrings[i - begin - 1] = String.valueOf(ints[i]);
            }
        }

        if (maxStrings == null) {
            maxStrings = new String[]{""};
        }
        wvMax.setViewAdapter(new ArrayWheelAdapter<String>(mContext, maxStrings));
        wvMax.setCurrentItem(0);

    }


    /**
     * 初始化数据
     */
    protected void initProvinceDatas() {
        minStrings = new String[ints.length];
        for (int i = 0; i < ints.length; i++) {
            if (i == 0)
                minStrings[i] = "不限";
            else
                minStrings[i] = String.valueOf(ints[i]);
        }
    }

    /**
     * 滚动监听
     *
     * @param wheel    the wheel view whose state has changed
     * @param oldValue the old value of current item
     * @param newValue the new value of current item
     */

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == wvMin) {
            updateCities();
        } else if (wheel == wvMax) {

            Log.d(TAG, "min == " + myMin + ",max == " + myMax);
        }
//        pickValue = myMin + divider + myMax + divider + mCurrentDistrictName;
        if (showOther)
            pickValue = myMin + divider + myMax;
        else
            pickValue = String.valueOf(myMin);
//        pickValue = myMin  + myMax  + mCurrentDistrictName;
    }

    public void setDivider(String divider) {
        this.divider = divider;
    }

    public interface RangePickListener {
        void pickValue(String value);
    }

    public void setCityPickListener(RangePickListener listener) {
        this.rangePickListner = listener;
    }

}
